home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / rng.exe / SAMPLE.C < prev   
C/C++ Source or Header  |  1991-12-19  |  1KB  |  46 lines

  1. /* sample.c - Illustrates how to use the random number generator and tests
  2. ** the Marsaglia operator to make sure it's working properly
  3. */
  4.  
  5. #include <stdio.h>
  6. #ifdef __STDC__
  7. #include <time.h>
  8. #endif
  9. #include "ran.h"
  10.  
  11. main() {
  12.    double dummy;              /* dummy variable used to store results */
  13.    double x[3];               /* used to store results to report */
  14. #ifdef __STDC__
  15.     double tm;                        /* used to store time */
  16. #endif
  17.    unsigned long seed;        /* seed used in random number generator */
  18.    int i;                     /* index variable */
  19.  
  20.    setup(&seed);                 /* r.n.g. must be initialized before use */
  21. #ifdef __STDC__
  22.     tm = (double)clock();
  23. #endif
  24.    for (i = 0; i < 20000; i++) {
  25.        dummy = ran();
  26.    }
  27. #ifdef __STDC__
  28.     tm = (double)clock() - tm;
  29. #endif
  30.    printf("The random numbers should be:\n");
  31.    printf("\t%.1f %.1f %.1f\n", 6533892.0, 14220222.0, 7275067.0);
  32.    printf("\t%.1f %.1f %.1f\n\n", 6172232.0, 8354498.0, 10633180.0);
  33.    printf("The results are:\n");
  34.    for (i = 0; i < 3; i++) {
  35.        x[i] = 4096.0*4096.0*ran();
  36.    }
  37.    printf("\t%.1f %.1f %.1f\n", x[0], x[1], x[2]);
  38.    for (i = 0; i < 3; i++) {
  39.        x[i] = 4096.0*4096.0*ran();
  40.    }
  41.    printf("\t%.1f %.1f %.1f\n", x[0], x[1], x[2]);
  42. #ifdef __STDC__
  43.     printf("\n%f microseconds per call\n", ((tm/CLOCKS_PER_SEC)/20000)*1000000);
  44. #endif
  45. }
  46.